package org.example.route.mapper;

import org.apache.ibatis.annotations.*;
import org.example.route.model.pojo.Carriage;

import java.util.List;

@Mapper
public interface CarriageMapper {
    @Insert("insert into carriage (train_id, carriage_number, volume, last_volume) VALUES " +
            "(#{trainId},#{carriageNumber},#{volume},#{lastVolume})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void add(Carriage carriage);

    @Update("update carriage set last_volume = last_volume - 1 where id = #{carriageId}")
    void beOccupied(Long carriageId);

    @Select("select train_id from carriage where id = #{carriageIdById}")
    Long getTrainIdById(Long carriageIdById);

    @Update("update carriage set last_volume = last_volume + 1 where id = #{carriageId}")
    void beReleased(Long carriageId);

    @Select("select * from carriage where train_id = #{trainId} and carriage_number = #{carriageNumber}")
    Carriage getByTrainIdAndCarriageNumber(Long trainId,Long carriageNumber);


    @Select("select count(*) from carriage where train_id = #{trainId}")
    Long getCarriageCountById(Long trainId);

    @Delete("delete from carriage where train_id = #{trainId}")
    void deleteByTrainId(Long trainId);


    @Select("select id from carriage where train_id = #{trainId}")
    List<Long> getIdsByTrainId(Long trainId);

    @Select("select carriage_number from carriage where id = #{carriageId}")
    Long getCarriageNumberById(Long carriageId);


    @Select("select * from carriage where train_id = #{trainId} and volume = #{volume}")
    List<Carriage> listByTrainIdAndGrade(Long trainId,Integer volume);
}
